package com.cetc;

import org.junit.Test;

import java.io.*;

/**
 * @Classname IOObjectStreamTest 对象输入输出流
 * @Description ObjectInputStream/ObjectOutputStream的使用
 * @Date 2022/11/25 10:39
 * @Created by louzhuangxi
 */
public class IOObjectStreamTest {
    @Test
    public void test01() {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            String str = "hello 中国";
            oos = new ObjectOutputStream(new FileOutputStream("data/hello.dat"));
            oos.writeObject(str);

            ois = new ObjectInputStream(new FileInputStream("data/hello.dat"));
            String str1 = (String) ois.readObject();
            System.out.println(str1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test02() {
        Person person = new Person(1L,"张三", 25);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("data/hello.dat"));
            oos.writeObject(person);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test03() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("data/hello.dat"));
            Person person = (Person) ois.readObject();
            System.out.println(person);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class Person implements Serializable {
    private Long id;
    private String name;
    private Integer age;

    public Person(Long id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }
}
